﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;


namespace Icinetic.FeedCategorySample.CrossCutting
{
    public class LoggingService
    {
        private static string filename = "log.txt";

        public static async void Log(string message)
        {
            #if DEBUG
            try
            {
                StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;

                if (!await folder.FileExistsAsync(filename))
                {
                    await folder.CreateFileAsync(filename);
                }

                StorageFile file = await folder.GetFileAsync(filename);

                string logMessage = string.Format("{0}\t{1}{2}", DateTime.Now, message, Environment.NewLine);
                await Windows.Storage.PathIO.AppendTextAsync(file.Path, logMessage);
            }
            catch { }

            #endif
        }

        public static async void Log(Exception ex)
        {
            #if DEBUG
            try
            {
                StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;

                if (!await folder.FileExistsAsync(filename))
                {
                    await folder.CreateFileAsync(filename);
                }

                StorageFile file = await folder.GetFileAsync(filename);

                string logMessage = string.Format("{0}\t{1}{2}Stack: {3}{2}", DateTime.Now, ex.Message, Environment.NewLine, ex.StackTrace);
                await Windows.Storage.PathIO.AppendTextAsync(file.Path, logMessage);
            }
            catch { }

            #endif
        }

        public static async void Log(string message, Exception ex)
        {
            #if DEBUG
            try
            {
                StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;

                if (!await folder.FileExistsAsync(filename))
                {
                    await folder.CreateFileAsync(filename);
                }

                StorageFile file = await folder.GetFileAsync(filename);

                string logMessage = string.Format("{0}\t{4}\t{1}{2}Stack: {3}{2}", DateTime.Now, ex.Message, Environment.NewLine, ex.StackTrace, message);
                await Windows.Storage.PathIO.AppendTextAsync(file.Path, logMessage);
            }
            catch { }

            #endif
        }
    }
}
